<?php
$servername = "localhost";
$username = "root";
$password = "dandan520";
$dbname = "xiaopi.com";
//创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
//检测连接
if (!$conn) {
    die("Connection failed:" . mysqli_connect_error());
}
//查询数据库
$sql = "select * from yonghu";

$res = mysqli_query($conn, $sql);
// if(1<2)
if ($res) {
    $user = [];
    // var_dump($res);
    while ($row = $res->fetch_assoc()) {
        $user[] = [
            'id' => $row['id'],
            'username' => $row['username'],
            'password' => $row['password']
        ];
    }
    $result['code'] = 200;
    $result['msg'] = '查询成功';
    $result['data'] = $user;
    echo json_encode($result);
} else {
    $result['code'] = -1;
    $result['msg'] = '查询失败' . 'Error:' . $sql . '<br>' . mysqli_error($conn);
    echo json_encode($result);
}
mysqli_close($conn);